#include "aceMesh_composite_task.h"
#include "utils/acelog.h"
#include "MemPool.h"

#ifdef TEMP_NESTED
#define NESTED_STACKSIZE 10
extern AceMesh_runtime::task_dag_graph task_graphs[NESTED_STACKSIZE];
extern int cur_nested_lev;
#endif
extern "C" char aceMesh_outdir[10];
extern int sche_num_threads;
#ifndef MEMORY_POOL
extern "C" void *my_malloc_aligned(int size);
#endif

void aceMesh_composite_task_constructor(aceMesh_composite_task *self, TASK_FUNCPTR funcptr)
{
    ci_task_constructor(&(self->ci_task_base), funcptr);
#ifdef TEMP_NESTED
    task_graphs[cur_nested_lev].init(sche_num_threads, aceMesh_outdir);
#else
    //placement new
    new (&(self->graph)) AceMesh_runtime::task_dag_graph();
    self->graph.init(sche_num_threads, aceMesh_outdir);
#endif
}

void aceMesh_composite_task_destructor(aceMesh_composite_task *self) {}

void define_with_taskptr(ci_task *self, void *a_args, unsigned int args_size,
                         void *taskptr)
{
    alog_debug("define_with_taskptr taskptr: %x", taskptr);

#ifdef MEMORY_POOL
#ifdef USE_SIMD
#ifdef TARG_SW5
    self->ci_args = acemesh_myalloc_aligned_32(args_size);
#elif TARG_SW9
    self->ci_args = acemesh_myalloc_aligned_64(args_size);
#endif
#else
    self->ci_args = acemesh_myalloc_aligned_16(args_size);
#endif
#else
#ifdef TARG_SW5
    self->ci_args = malloc(args_size);
#elif TARG_SW9
    //self->ci_args = libc_uncached_aligned_malloc(args_size);
    self->ci_args = my_malloc_aligned(args_size);
#else
    assert(0);
#endif 
#endif
    memcpy(self->ci_args, a_args, args_size);
}

void set_sub_affinity_id(AceMesh_runtime::affinity_id_generator_range *affinity_gen, int startid,
                         int mapsize)
{
    affinity_gen->set_sub_affinity(startid, mapsize);
}
